################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		January 1, 2018
#
################################
# histogram of institutional authority for redistricting in states for legislative offices: 2000 and 2010

rm(list=ls())
redist=read.csv('~/Dropbox/StateRedistricting/replication/short/redist_authority.csv',header=T,stringsAsFactors=F)

# histogram
i2000=which(redist[,1]==2000)
i2010=which(redist[,1]==2010) 

# latex table: 2000   
states=congs=array(NA,nrow(redist))
for(i in 1:nrow(redist)){ 
	if(length(which(redist[i,c(3,5:9)]==1))>0){
		congs[i]=paste(names(redist[i,c(3,5:9)])[which(redist[i,c(3,5:9)]==1)],collapse='-')	
	}
	if(length(which(redist[i,c(10,12:16)]==1))>0){ 
		states[i]=paste(names(redist[i,c(10,12:16)])[which(redist[i,c(10,12:16)]==1)],collapse='-')
	}
}

# histogram of 
# congress & state || 2000 & 2010
#  legis + D
#  legis + R
#  legis + split & politicial/backup commission
#  court plans
#  independent commission 
   
# congress 2000                       
ii3=grep(congs[i2000],pattern='backup')
ii3=c(ii3,grep(congs[i2000],pattern='politician')) 

ii4=sort(grep(congs[i2000],pattern='court'))
ii5=sort(grep(congs[i2000],pattern='independent'))
                                                                   
ii0=i2000[-c(sort(c(ii4,ii5,ii3)))]
ii0=ii0[which(!is.na(redist[ii0,3]))]  

ii1=sort(ii0[which(redist$congress_legislatue_partycontrol[ii0]=='D')])
ii2=sort(ii0[which(redist$congress_legislatue_partycontrol[ii0]=='R')]) 

ii3=sort(unique(c(ii3,ii0[which(redist$congress_legislatue_partycontrol[ii0]!='R' & redist$congress_legislatue_partycontrol[ii0]!='D')])))

c2000=list()
c2000[[1]]=ii1
c2000[[2]]=ii2
c2000[[3]]=ii3
c2000[[4]]=ii4
c2000[[5]]=ii5

# congress 2010
ii3=grep(congs[i2010],pattern='backup')
ii3=c(ii3,grep(congs[i2010],pattern='politician')) 

ii4=sort(grep(congs[i2010],pattern='court'))
ii5=sort(grep(congs[i2010],pattern='independent'))
                                                                   
ii0=i2010[-c(sort(c(ii4,ii5,ii3)))]-50
ii0=ii0[which(!is.na(redist[ii0,3]))]  

ii1=sort(ii0[which(redist[i2010,]$congress_legislatue_partycontrol[ii0]=='D')])
ii2=sort(ii0[which(redist[i2010,]$congress_legislatue_partycontrol[ii0]=='R')]) 

ii3=sort(unique(c(ii3,ii0[which(redist[i2010,]$congress_legislatue_partycontrol[ii0]!='R' & redist[i2010,]$congress_legislatue_partycontrol[ii0]!='D')])))

c2010=list()
c2010[[1]]=ii1
c2010[[2]]=ii2
c2010[[3]]=ii3
c2010[[4]]=ii4
c2010[[5]]=ii5
    
# state 2000                       
ii3=grep(states[i2000],pattern='backup')
ii3=c(ii3,grep(states[i2000],pattern='politician')) 

ii4=sort(grep(states[i2000],pattern='court'))
ii5=sort(grep(states[i2000],pattern='independent'))
                                                                   
ii0=i2000[-c(sort(c(ii4,ii5,ii3)))]
ii0=ii0[which(!is.na(redist[ii0,3]))]  

ii1=sort(ii0[which(redist$state_legislature_partycontrol[ii0]=='D')])
ii2=sort(ii0[which(redist$state_legislature_partycontrol[ii0]=='R')]) 

ii3=sort(unique(c(ii3,ii0[which(redist$state_legislatue_partycontrol[ii0]!='R' & redist$state_legislatue_partycontrol[ii0]!='D')])))

s2000=list()
s2000[[1]]=ii1
s2000[[2]]=ii2
s2000[[3]]=ii3
s2000[[4]]=ii4
s2000[[5]]=ii5

# state 2010
ii3=grep(states[i2010],pattern='backup')
ii3=c(ii3,grep(states[i2010],pattern='politician')) 

ii4=sort(grep(states[i2010],pattern='court'))
ii5=sort(grep(states[i2010],pattern='independent'))
                                                                   
ii0=i2010[-c(sort(c(ii4,ii5,ii3)))]-50
ii0=ii0[which(!is.na(redist[ii0,3]))]  

ii1=sort(ii0[which(redist[i2010,]$state_legislature_partycontrol[ii0]=='D')])
ii2=sort(ii0[which(redist[i2010,]$state_legislature_partycontrol[ii0]=='R')]) 

ii3=sort(unique(c(ii3,ii0[which(redist[i2010,]$state_legislatue_partycontrol[ii0]!='R' & redist[i2010,]$state_legislatue_partycontrol[ii0]!='D')])))

s2010=list()
s2010[[1]]=ii1
s2010[[2]]=ii2
s2010[[3]]=ii3
s2010[[4]]=ii4
s2010[[5]]=ii5
 

# congress & state
counts_state=counts_congs=matrix(NA,5,2)
for(j in 1:5){
	counts_congs[j,]=c(length(c2000[[j]]),length(c2010[[j]]))
	counts_state[j,]=c(length(s2000[[j]]),length(s2010[[j]]))	
}

counts_congs=t(counts_congs) 
counts_state=t(counts_state) 
colnames(counts_congs)=colnames(counts_state)=c('Democratic','Republican','Bipartisan','Court Plan','Independent')
                                    
#cols=c('lightblue','dodgerblue4')       #c('lightblue','dodgerblue4')
#hist_educ_level.pdf # blue scale       
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_states.pdf')  
cols=c('lightblue','dodgerblue4') 
barplot(counts_state, main="",  cex.axis = 1, cex.names = .87,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_state), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols)
dev.off()       
           
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_congs.pdf')  
cols=c('lightblue','dodgerblue4') 
barplot(counts_congs, main="",  cex.axis = 1, cex.names = .87,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_congs), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols)
dev.off()
  
# grey
#cols=c('lightblue','dodgerblue4')       #c('lightblue','dodgerblue4')
#hist_educ_level.pdf # blue scale       
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_states_grey.pdf')  
cols=c('lightgrey','grey42')   
barplot(counts_state, main="",  cex.axis = 1, cex.names = .87,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_state), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols)
dev.off()       
           
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_congs_grey.pdf')  
cols=c('lightgrey','grey42')    
barplot(counts_congs, main="",  cex.axis = 1, cex.names = .87,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_congs), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols)
dev.off()
 
# bw
#cols=c('lightblue','dodgerblue4')       #c('lightblue','dodgerblue4')
#hist_educ_level.pdf # blue scale       
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_states_bw.pdf')   
dens=c(15,80)
cols=c('black','black')   
barplot(counts_state, main="",  cex.axis = 1, cex.names = .87, density=dens,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_state), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols,density=dens)
dev.off()       
           
pdf(file='~/Dropbox/StateRedistricting/replication/short/hist_congs_bw.pdf')  
cols=c('black','black')   
barplot(counts_congs, main="",  cex.axis = 1, cex.names = .87, density=dens,
   col=cols,xlab='Redistricting Type',ylab='Counts',
   legend = rownames(counts_congs), beside=T,space=c(0,0,.375,0,.375,0,.375,0,.375,0),ylim=c(0,20))                     
legend(cex=1.2,x=-.1,y=20,legend=c('2000 Redistricting','2010 Redistricting'),col=cols,fill=cols,density=dens)
dev.off()

   
# table    
#cong-2000
redist[c2000[[1]],2]
redist[c2000[[2]],2]
redist[c2000[[3]],2]
redist[c2000[[4]],2]
redist[c2000[[5]],2]
  
#cong-2010
redist[c2010[[1]],2]
redist[c2010[[2]],2]
redist[c2010[[3]],2]
redist[c2010[[4]],2]
redist[c2010[[5]],2]  
         
#state-2000
redist[s2000[[1]],2]
redist[s2000[[2]],2]
redist[s2000[[3]],2]
redist[s2000[[4]],2]
redist[s2000[[5]],2]
  
#state-2010
redist[s2010[[1]],2]
redist[s2010[[2]],2]
redist[s2010[[3]],2]
redist[s2010[[4]],2]
redist[s2010[[5]],2]  

#end